// pages/Personal/recharge/recharge.js
var app = getApp();
var ikz = require('../../Common/tools.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
  
  },
  goback: function () {
    wx.navigateBack()
  },
  //提交
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value);
    var recharge_num = e.detail.value.recharge_num;
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if (reg.test(recharge_num) && recharge_num > 0) {
      wx.showModal({
        title: '提示',
        content: '您确认充值吗？',
        success: function (res) {
          if (res.confirm) {
            var that = this;
            var path = 'Percenter/goRecharge';
            var data = {
              recharge_num: recharge_num,
            }
            ikz.request(path, data, true, (res) => {
              console.log(res)
              if (res.data.status == 'success') {
                var payment = res.data.pay;
                var outnum = res.data.out_num;
                ikz.pay(payment, (res) => {
                  //提示 并跳转
                  wx.showModal({
                    title: '提示',
                    content: '查看支付结果',
                    success: function (res) {
                      console.log(res);
                      var path = 'Percenter/checkOr';
                      var data = {
                        num: outnum
                      }
                      ikz.request(path, data, true, (res) => {
                        if (res.data == 'success') {
                          wx.showModal({
                            title: '提示',
                            content: '支付成功',
                            showCancel: false
                          })
                        } else {
                          wx.showModal({
                            title: '提示',
                            content: '支付失败，请重试',
                            showCancel: false
                          })
                        }
                      })
                    }
                  })
                })
              } else {
                wx.showModal({
                  title: '提示',
                  content: '服务器繁忙，请稍后重试！',
                  showCancel: false
                })
              }

            });
          } else if (res.cancel) {
            return false;
          }
        }
      })
    } else {
      wx.showToast({
        title: '请正确填写金额',
        duration: 2000
      });
      return false;
    };
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    ikz.over(this);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    ikz.hide(this);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})